/**
 * 公共函数
 * @module LJF
 */
let LJF = {
    /**
     * @description obj转url参数
     * @function
     * @name urlParams
     * @param {object} argParams 参数对象
     * @param {boolean} noMark 是否添加'?'
     * @returns {string}
     */
    urlParams(argParams, noMark) {
        let re = ''
        if (!noMark) {
            re = '?'
        }
        for (let k in argParams) {
            re += k + '=' + argParams[k] + '&'
        }
        if (argParams) {
            re = re.substring(0, re.length - 1)
        }
        return re
    },
    /**
     * @description 日期格式化显示
     * @function
     * @name formatTime
     * @param  {number} date 时间对象\时间戳，默认当前时间
     * @param  {string} fmt 格式化符串，默认'YYYY-MM-DD HH:mm:ss'
     * @returns {string}
     */
    formatTime(date = +new Date(), fmt = 'YYYY-MM-DD HH:mm:ss') {
        date = new Date(+date)
        var o = {
            'M+': date.getMonth() + 1,
            'D+': date.getDate(),
            'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
            'H+': date.getHours(),
            'm+': date.getMinutes(),
            's+': date.getSeconds(),
            'q+': Math.floor((date.getMonth() + 3) / 3),
            'S': date.getMilliseconds()
        }
        var week = {
            '0': '\u65e5',
            '1': '\u4e00',
            '2': '\u4e8c',
            '3': '\u4e09',
            '4': '\u56db',
            '5': '\u4e94',
            '6': '\u516d'
        }
        if (/(Y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
        }
        if (/(E+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') + week[date.getDay() + ''])
        }
        for (var k in o) {
            if (new RegExp('(' + k + ')').test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
            }
        }
        return fmt
    },
    /**
     * @description 日期格式化友好显示 刚刚 x分钟前 ...
     * @function
     * @name friendlyTime
     * @param  {number} date 时间对象\时间戳，默认当前时间
     * @param  {string} fmt 格式化符串，默认'YYYY-MM-DD HH:mm:ss'
     * @returns {string}
     */
    friendlyTime(date = +new Date(), fmt = 'YYYY-MM-DD HH:mm:ss') {
        date = new Date(+date)
        let diff = (((new Date()).getTime() - date.getTime()) / 1000)
        let dayDiff = Math.floor(diff / 86400)
        let isValidDate = Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime())
        if (!isValidDate) {
            console.error('not a valid date')
        }
        if (isNaN(dayDiff) || dayDiff < 0 || dayDiff >= 31) {
            return this.formatTime(date, fmt)
        }

        return dayDiff === 0 && (
                diff < 60 && '刚刚' ||
                diff < 120 && '1分钟前' ||
                diff < 3600 && Math.floor(diff / 60) + '分钟前' ||
                diff < 7200 && '1小时前' ||
                diff < 86400 && Math.floor(diff / 3600) + '小时前') ||
            dayDiff === 1 && '昨天' ||
            dayDiff < 7 && dayDiff + '天前' ||
            dayDiff < 31 && Math.ceil(dayDiff / 7) + '周前'
    }
}
export default LJF